home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11428 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.3 KB  |  133 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Newbie Questions
  5. Date: Sun, 24 Mar 96 00:47:36 GMT
  6. Organization: none
  7. Message-ID: <827628456snz@genesis.demon.co.uk>
  8. References: <4irpc1$b8u@GRAPEVINE.LCS.MIT.EDU> <4isgta$7cr@newsbf02.news.aol.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4isgta$7cr@newsbf02.news.aol.com>
  15.            mvaccaro1@aol.com "MVaccaro1" writes:
  16.  
  17. >>Question #2
  18. >>
  19. >>Why does the scope of the external declaration not extend beyond >the
  20. >>inner block?
  21.  
  22. The extern keyword has no effect on scope (i.e. where in the source file
  23. a particular declaration is visible). It can affect linkage (i.e. whether
  24. two declarations for the same identifier refer to the same object/function),
  25. duration (i.e. it determines or at least indicates when the object the
  26. declaration refers to is created and destroyed). It can also prevent an
  27. object declaration becoming a definition.
  28.  
  29. The terminology can be confusing here since an 'external declaration'
  30. simply means one that appears outside of a function i.e. at file scope and
  31. has nothing to do with the extern keyword at all.
  32.  
  33. >>{
  34. >>    {
  35. >>        extern E;
  36. >>        E = 0;
  37. >>    }
  38. >>    E = 1;
  39. >>}
  40. >
  41. >Your declare of the external variable E is only known in the block it is
  42. >declared in.  Had you wrote the code this way:
  43. >
  44. >extern int E;
  45. >
  46. >func( ... )
  47. >{
  48. >   {
  49. >   E=0;
  50. >   }
  51. >E=1;
  52. >}
  53. >
  54. >E would be known to all references in the file.
  55. >
  56. >Where as 
  57. >
  58. >func( ... )
  59. >   {
  60. >   extern int E;
  61. >      {
  62. >      E = 0;
  63. >      }
  64. >   E = 1;
  65. >   }
  66. >
  67. >E would only be known within function.
  68.  
  69. The interesting case is:
  70.  
  71. int E;                   /* a */
  72.  
  73. func( ... )
  74. {
  75.    int E;                /* b */
  76.  
  77.       {
  78.          extern int E;   /* c */
  79.          E=0;
  80.       }
  81.  
  82.    E=1;
  83. }
  84.  
  85. The declarations a and c (and hence the E=0) refer to the same object because
  86. of linkage. b (and the E=1) refer to an entirely separate local variable
  87. in the function.
  88.  
  89. >>Question #3
  90. >>
  91. >>Is there any difference between these two declarations?
  92. >>
  93. >>   char *s1 = "hello";
  94.  
  95. The creates a static array of char initialised to
  96. { 'h', 'e', 'l', 'l', 'o', '\0' }. s1 itself is defined as a char * pointer
  97. and initialised to point to the first character of the array. s1 is a
  98. normal variable that you can modify, it is illegal to modify the character
  99. array.
  100.  
  101. >>   char s2[] = "hello";
  102.  
  103.  
  104. Here s2 is defined to be an array of char initialised to
  105. { 'h', 'e', 'l', 'l', 'o', '\0' }. You can modify the elements of the
  106. array but there is no pointer here to modify as there is with s1.
  107.  
  108. >try this little test and tell me what happens:
  109. >
  110. >main( )
  111. >   {
  112. >   char *s1 = "hello";
  113. >   char s2[ ] = "hello";
  114. >
  115. >   printf( "Sizeof s1 %d\n Sizeof s2 %d\n", sizeof s1, sizeof s2 );
  116.  
  117. Anything can happen since sizeof doesn't return an int value, it returns
  118. a value with some implementation specific unsigned type (size_t). The
  119. correct way to write this is:
  120.  
  121.     printf( "Sizeof s1 %lu\n Sizeof s2 %lu\n", (unsigned long) sizeof s1,
  122.                                                (unsigned long) sizeof s2 );
  123.  
  124.  
  125. >   return 0;
  126. >   }
  127.  
  128. -- 
  129. -----------------------------------------
  130. Lawrence Kirby | fred@genesis.demon.co.uk
  131. Wilts, England | 70734.126@compuserve.com
  132. -----------------------------------------
  133.